package com.file;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.junit.Test;
import org.springframework.util.FileCopyUtils;


import java.io.*;


@Slf4j
public class FtpClientUtil {

    public static final String FILE_NAME = "Mysql_extract_dell_20190401.sql.gz";
    private String host="60.205.214.212";
    private int port=110;
    private String userName="extract_r";
    private String psd="t2ooKz8PlSrNdMoQ";
    private   FTPClient ftpClient = new FTPClient();

    private  String localPath="D:\\工作文档\\sinobase\\需求文档\\戴尔";
    @Test
    public    void init() throws IOException {
        ftpClient.connect(host);
        ftpClient.login(userName,psd);
        FTPFile[] ftpFiles = ftpClient.listFiles();
        for (FTPFile ftpFile : ftpFiles) {
              String name = ftpFile.getName();
            log.info("---"+name);
              if(name.equals(FILE_NAME)){
                  InputStream inputStream = ftpClient.retrieveFileStream(FILE_NAME);
                  File file=new File(localPath, FILE_NAME);
//                  file.createNewFile();
                  FileOutputStream fileOutputStream = new FileOutputStream(file);
                  int copy = FileCopyUtils.copy(inputStream, fileOutputStream);
                  log.info("---"+copy);

              }

        }



        ftpClient.logout();
    }


}
